package com.dan.inter.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dan.inter.constants.AliyunConstants;
import com.dan.inter.constants.Constants;
import com.dan.inter.service.ALiYunApiService;
import com.dan.inter.util.GetImage;
import com.dan.inter.util.PerUtil;
import com.dan.inter.util.aliyun.Base64Pict;
import com.dan.inter.util.aliyun.HttpUtils;
import com.dan.inter.util.aliyun.constants.HttpMethod;
import com.dan.inter.vo.ALiyunFaceAgeVO;

@Service
public class ALiYunApiServiceImpl implements ALiYunApiService {

	private static final Log LOG = LogFactory.getLog(ALiYunApiServiceImpl.class);
	
	@Override
	public String faceRecognition(String imgFile) {
		String downloadImage = downloadImage(imgFile);
		String ImageStr = Base64Pict.GetImageStr(downloadImage);
		
		String host = AliyunConstants.host_dm_21;
		String path = AliyunConstants.path_face_detection;
		String method = HttpMethod.POST;
		String appcode = AliyunConstants.appcode;
		Map<String, String> headers = new HashMap<String, String>();
		// 最后在header中的格式(中间是英文空格)为Authorization:APPCODE
		// 83359fd73fe94948385f570e3c139105
		headers.put("Authorization", "APPCODE " + appcode);
		// 根据API的要求，定义相对应的Content-Type
		headers.put("Content-Type", "application/json; charset=UTF-8");
		Map<String, String> querys = new HashMap<String, String>();
		String bodys = "{\"inputs\":[{\"image\":{\"dataType\":50,\"dataValue\":\""+ImageStr+"\"}}]}";
		HttpResponse response = null;
		try {
			response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
			System.out.println(response.toString());
			// 获取response的body
			// System.out.println(EntityUtils.toString(response.getEntity()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		String result = PerUtil.responseOutWithJson(response);
		return result;
	}
	
	public String downloadImage(String url){
        byte[] btImg = GetImage.getImageFromNetByUrl(url);  
        String fileName = null;
        if(null != btImg && btImg.length > 0){  
        	LOG.info("读取到：" + btImg.length + " 字节");
        	int randomNum = PerUtil.randomNum(100000);
            fileName = randomNum+".jpg";
            GetImage.writeImageToDisk(btImg, fileName);  
        }else{
        	LOG.info("没有从该连接获得内容");
        }  
		return "G:\\aliyunImage\\" + fileName;
	}

	@Override
	public String faceAgeRecognition(ALiyunFaceAgeVO aLiyunFaceAgeVO) {
		String ImageStr = Base64Pict.GetImageStr(aLiyunFaceAgeVO.getImageData());
		String host = AliyunConstants.host_dm_23;
	    String path = AliyunConstants.path_face_age_detection;
	    String method = HttpMethod.POST;
	    String appcode = aLiyunFaceAgeVO.getAppcode();
	    Map<String, String> headers = new HashMap<String, String>();
	    headers.put("Authorization", "APPCODE " + appcode);
	    headers.put("Content-Type", "application/json; charset=UTF-8");
	    Map<String, String> querys = new HashMap<String, String>();
	    String bodys = "{\"inputs\":[{\"image\":{\"dataType\":50,\"dataValue\":\""+ImageStr+"\"}}]}";
	    HttpResponse response = null;
	    try {
	    	response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
	    	System.out.println(response.toString());
	    	//获取response的body
	    	//System.out.println(EntityUtils.toString(response.getEntity()));
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }
	    String result = PerUtil.responseOutWithJson(response);
		return result;
	}

	@Override
	public String faceAgeConvert(String response) {
		JSONObject parseObject = JSONObject.parseObject(response);
		JSONArray outputsJsonArray = parseObject.getJSONArray("outputs");
		
//		JSONObject jsonArray_node = outputsJsonArray.getJSONObject(0);
		Object node = outputsJsonArray.get(0);
		String node_var = node.toString();
		
		JSONObject node_varObject = JSONObject.parseObject(node_var);
		String outputValue = node_varObject.getString("outputValue");
		JSONObject outputValueJSONObject = JSONObject.parseObject(outputValue);
		String dataValue = outputValueJSONObject.getString("dataValue");
		JSONObject dataValueJSONObject = JSONObject.parseObject(dataValue);
		JSONArray ageJsonArray = dataValueJSONObject.getJSONArray("age");
		if (ageJsonArray.size()==Constants.Integer_one) {
			Object object = ageJsonArray.get(0);
			return object.toString();
		} else {

		}
		return null;
	}

}
